package org.xiaochangbai.idea.plugin.form;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;

import javax.annotation.Nullable;
import javax.swing.*;
import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author xiaochangbai
 * @since 2022/7/1 10:32
 */
public class FormTestDialog extends DialogWrapper {
    
    private String projectName;
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    private JLabel r2 = new JLabel();
    
    private volatile String closeWorkingTime = "18:30";
    
    
    public FormTestDialog(@Nullable Project project) {
        super(true);
        //设置会话框标题
        setTitle("Countdown to work");
        //获取到当前项目的名称
        this.projectName = project.getName();
        //触发一下init方法，否则swing样式将无法展示在会话框
        init();
    }
    
    
    @Override
    protected JComponent createNorthPanel() {
        //定义表单的主体部分，放置到IDEA会话框的中央位置
        JPanel center = new JPanel();
        //一个简单的3行2列的表格布局
        center.setLayout(new GridLayout(1, 3));
    
        //row1：按钮事件触发后将结果打印在这里
        JLabel r1 = new JLabel("My off work time：");
        r1.setForeground(new Color(194, 43, 180)); //设置字体颜色
        
        JTextField timeContext = new JTextField();
        timeContext.setText(closeWorkingTime);
        
        JButton submit = new JButton("modify");
        submit.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        submit.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        submit.addActionListener(e->{
            String text = timeContext.getText();
            closeWorkingTime = text;
        });
        
        center.add(r1);
        center.add(timeContext);
        center.add(submit);
        return center;
    }
    
    
    @Override
    protected JComponent createCenterPanel() {
        JPanel jPanel = new JPanel();
        JLabel r1 = new JLabel("Distance from work：");
        r1.setForeground(new Color(194, 43, 61)); //设置字体颜色
        jPanel.add(r1);
        calculateTime();
        jPanel.add(r2);
        Thread thread = new Thread(() -> {
            while(true){
                calculateTime();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
        return jPanel;
    }
    
    private void calculateTime() {
        Calendar calendar = Calendar.getInstance();
        int weekYear = calendar.getWeekYear();
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DATE);
        String[] strings = closeWorkingTime.split(":");
        String format = String.format("%d-%02d-%02d %02d:%02d:%02d", weekYear, month, day,Integer.valueOf(strings[0]),Integer.valueOf(strings[1]),0);
        try {
            Date parse = sdf.parse(format);
            long time1 = parse.getTime()-System.currentTimeMillis();
            int sen = (int) (time1/1000/60);
            r2.setText(sen + " minute");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
}